<?php
include('../includes/classes/core.php');
include('../includes/classes/logger.class.php');

$action	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID1'];
$error	= '';
$data	= '';

switch ($action) {
    case 'get_list':
        $wr='';
    	$l='ბ';
    	$l2='ე';
    	$date="";
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    $tab = $_REQUEST['tab']+1;
 		switch ($tab){
 			case 1:
 				$date="IFNULL(d_letter.request_date,a_letter.`request_date`),";
 				$l='ა';
 				$l2='დ';
 			break;
 			case 2:
 				$date="IFNULL(d_letter.start_date,a_letter.`start_date`),";
 				$l='ა';
 				$l2='დ';
 			break;
 			case 4:
 				$date="IFNULL(d_letter.fact_end_date, a_letter.`fact_end_date`),";
 			break;
 			case 5:
 				$date="IFNULL(d_letter.fact_end_date, a_letter.`fact_end_date`),";
 			break;
 			case 6:
 				$date="rs_wdsl_status.end_date,";
 				//$join="LEFT JOIN rs_wdsl_status ON rs_wdsl_status.id=IFNULL(d_letter.e_letter_overhead_id,a_letter.b_letter_overhead_id),";
 				//$wr="and (d_letter.finish_date > 0 or a_letter.`finish_date`>0)";
 			break;

 		};
	    $rResult = mysql_query(" SELECT 			IF(ISNULL(warehouse.a_letter_id),d_letter.id,a_letter.`id`),
	    											$date
													IF(ISNULL(warehouse.a_letter_id),CONCAT('$l2 – ',d_letter.`id`),CONCAT('$l – ',a_letter.`id`)),
													CONCAT(client.`name`,' / ',client_objects.`name`) as client

								FROM			warehouse
								LEFT JOIN a_letter ON a_letter.id = warehouse.a_letter_id AND a_letter.actived=1
								LEFT JOIN d_letter ON d_letter.id = warehouse.d_letter_id AND d_letter.actived=1
	    						LEFT JOIN client_web_users ON client_web_users.id=$user_id
								     JOIN request_status ON request_status.id = IF(ISNULL(warehouse.a_letter_id),d_letter.`status`,a_letter.`status`)
								LEFT JOIN rs_wdsl_status ON rs_wdsl_status.id=IFNULL(d_letter.e_letter_overhead_id,a_letter.b_letter_overhead_id) 
	                            LEFT JOIN client_objects ON client_objects.id = warehouse.client_object_id
								LEFT JOIN client ON client.id = client_objects.client_id
	    						WHERE request_status.id=$tab AND client.id=client_web_users.client_id $wr
	    						ORDER BY warehouse.start_date DESC
	    		");

		$data = array( "aaData"	=> array());

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$data['aaData'][] = $aRow;
		}

        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);

?>